package com.jlastu.znb.message;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ListView messageListView;
    private EditText messageEditText;
    private Button sendButton;
    private List<Message> messages;
    private MessageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message);

        messageListView = findViewById(R.id.messageListView);
        messageEditText = findViewById(R.id.messageEditText);
        sendButton = findViewById(R.id.sendButton);

        messages = new ArrayList<>();
        adapter = new MessageAdapter(this, messages);
        messageListView.setAdapter(adapter);

        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = messageEditText.getText().toString();
                if (!message.isEmpty()) {
                    // 假设用户的姓名是"John"
                    String userSenderName = "John";
                    messages.add(new Message(message, userSenderName, false)); // 用户发送的消息
                    adapter.notifyDataSetChanged();
                    messageListView.setSelection(messages.size() - 1);
                    messageEditText.setText("");
                }
            }
        });
    }
}